/**
 * Copyright(c) 2019 Shenzhen Muchinfo CO, LTD.
 * All Rights Reserved.
 * @file       MessageProcImpl.h
 * @author     zou.yingbin
 * @Create      : 2019-9-24 9:14:34
 * @Modified    : 2019-9-24 9:14:34
 * @version    1.0
 * @note       
 */
 
#ifndef _MESSAGEPROCIMPL_H_
#define _MESSAGEPROCIMPL_H_
#include "MessageElement.h"

BEGIN_NAMESPACE_MTP

template<class Derived,
    uint32_t Funcode,
    typename ReqProto
>
class MessageProcImpl : public std::enable_shared_from_this < Derived >
{
public:
    using SelfMessageImpl = MessageProcImpl <
        Derived,
        Funcode,
        ReqProto > ;

    explicit MessageProcImpl(const MessageElement &msg)
    {
        _msg = msg;
    }
protected:
    MessageElement _msg;
    ReqProto _reqProto;
protected:
    virtual bool on_process(ReqProto &proto) = 0;
public:
    static inline const uint32_t GetFuncode()
    {
        return Funcode;
    }

    void on_message()
    {
        if (!_reqProto.ParseFromArray(_msg.content_ptr(), (int)_msg.content_size()))
        {
            int nFunCode = _msg.function_code;
            LOG_DEBUG("unpack fail, funcode:%d", nFunCode);
        }

        on_process(_reqProto);
    }
};

END_NAMESPACE_MTP

#endif //_MESSAGEPROCIMPL_H_
